<!DOCTYPE html>



  


<html class="theme-next gemini use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>
<meta name="theme-color" content="#222">









<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />
















  
  
  <link href="/lib/fancybox/source/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=5.1.4" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=5.1.4">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=5.1.4">


  <link rel="mask-icon" href="/images/logo.svg?v=5.1.4" color="#222">


  <link rel="manifest" href="/images/manifest.json">




  <meta name="keywords" content="技术,微前端,架构," />










<meta name="description" content="搭架自己的微前端软件设计原则solid额外设计原则 组合&#x2F;聚合复用原则  组合优于继承 23种设计模式   无环依赖  A-&gt;B-&gt;C-&gt;A 中介者模式   共同封装  将变化隔离 开放-封闭的拓展   共同重用  好莱坞原则  控制反转（依赖注入）npm包的思想   dry原则（不重复）  利用以上的设计原则去做封装   保持简单和傻瓜  保持页面简洁 功能实用 操作方便">
<meta property="og:type" content="article">
<meta property="og:title" content="搭架自己的微前端">
<meta property="og:url" content="https://swliu920322.github.io/2021/05/08/%E5%B7%A5%E7%A8%8B%E5%8C%96/%E5%BE%AE%E5%89%8D%E7%AB%AF/%E5%BE%AE%E5%89%8D%E7%AB%AF-%E6%9E%B6%E6%9E%84%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="技术博客">
<meta property="og:description" content="搭架自己的微前端软件设计原则solid额外设计原则 组合&#x2F;聚合复用原则  组合优于继承 23种设计模式   无环依赖  A-&gt;B-&gt;C-&gt;A 中介者模式   共同封装  将变化隔离 开放-封闭的拓展   共同重用  好莱坞原则  控制反转（依赖注入）npm包的思想   dry原则（不重复）  利用以上的设计原则去做封装   保持简单和傻瓜  保持页面简洁 功能实用 操作方便">
<meta property="og:locale">
<meta property="og:image" content="https://swliu920322.github.io/images/%E5%B7%A5%E7%A8%8B%E5%8C%96/%E5%BE%AE%E5%89%8D%E7%AB%AF%E6%9E%B6%E6%9E%84%E5%9B%BE.png">
<meta property="article:published_time" content="2021-05-08T13:30:47.000Z">
<meta property="article:modified_time" content="2022-05-13T08:59:17.000Z">
<meta property="article:author" content="刘圣伟">
<meta property="article:tag" content="技术">
<meta property="article:tag" content="微前端">
<meta property="article:tag" content="架构">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://swliu920322.github.io/images/%E5%B7%A5%E7%A8%8B%E5%8C%96/%E5%BE%AE%E5%89%8D%E7%AB%AF%E6%9E%B6%E6%9E%84%E5%9B%BE.png">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '',
    scheme: 'Gemini',
    version: '5.1.4',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":true,"scrollpercent":true,"onmobile":false},
    fancybox: true,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>







  <title>搭架自己的微前端 | 技术博客</title>
  





  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?91e3fae98446602c6fb71cc804c6ef9f";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




<meta name="generator" content="Hexo 5.4.0"></head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">技术博客</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description">刘圣伟</h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-about">
          <a href="/about/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />
            
            关于
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/tags/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      
        
        <li class="menu-item menu-item-categories">
          <a href="/categories/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-th"></i> <br />
            
            分类
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/archives/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-schedule">
          <a href="/schedule/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-calendar"></i> <br />
            
            日程表
          </a>
        </li>
      
        
        <li class="menu-item menu-item-sitemap">
          <a href="/baidusitemap.xml" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-sitemap"></i> <br />
            
            站点地图
          </a>
        </li>
      
        
        <li class="menu-item menu-item-commonweal">
          <a href="/404" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-heartbeat"></i> <br />
            
            公益404
          </a>
        </li>
      

      
        <li class="menu-item menu-item-search">
          
            <a href="javascript:;" class="popup-trigger">
          
            
              <i class="menu-item-icon fa fa-search fa-fw"></i> <br />
            
            搜索
          </a>
        </li>
      
    </ul>
  

  
    <div class="site-search">
      
  <div class="popup search-popup local-search-popup">
  <div class="local-search-header clearfix">
    <span class="search-icon">
      <i class="fa fa-search"></i>
    </span>
    <span class="popup-btn-close">
      <i class="fa fa-times-circle"></i>
    </span>
    <div class="local-search-input-wrapper">
      <input autocomplete="off"
             placeholder="搜索..." spellcheck="false"
             type="text" id="local-search-input">
    </div>
  </div>
  <div id="local-search-result"></div>
</div>



    </div>
  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="https://swliu920322.github.io/2021/05/08/%E5%B7%A5%E7%A8%8B%E5%8C%96/%E5%BE%AE%E5%89%8D%E7%AB%AF/%E5%BE%AE%E5%89%8D%E7%AB%AF-%E6%9E%B6%E6%9E%84%E5%9F%BA%E7%A1%80/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/%5Bobject%20Object%5D">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="技术博客">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">搭架自己的微前端</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2021-05-08T21:30:47+08:00">
                2021-05-08
              </time>
            

            

            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">分类于</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
                  <a href="/categories/%E5%BE%AE%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index">
                    <span itemprop="name">微前端</span>
                  </a>
                </span>

                
                
              
            </span>
          

          
            
              <span class="post-comments-count">
                <span class="post-meta-divider">|</span>
                <span class="post-meta-item-icon">
                  <i class="fa fa-comment-o"></i>
                </span>
                <a href="/2021/05/08/%E5%B7%A5%E7%A8%8B%E5%8C%96/%E5%BE%AE%E5%89%8D%E7%AB%AF/%E5%BE%AE%E5%89%8D%E7%AB%AF-%E6%9E%B6%E6%9E%84%E5%9F%BA%E7%A1%80/#comments" itemprop="discussionUrl">
                  <span class="post-comments-count valine-comment-count" data-xid="/2021/05/08/%E5%B7%A5%E7%A8%8B%E5%8C%96/%E5%BE%AE%E5%89%8D%E7%AB%AF/%E5%BE%AE%E5%89%8D%E7%AB%AF-%E6%9E%B6%E6%9E%84%E5%9F%BA%E7%A1%80/" itemprop="commentCount"></span> 条回复
                </a>
              </span>
            
          

          
          

          
            <span class="post-meta-divider">|</span>
            <span class="page-pv"><i class="fa fa-file-o"></i>
            <span class="busuanzi-value" id="busuanzi_value_page_pv" ></span>次已读
            </span>
          

          
            <div class="post-wordcount">
              
                
                <span class="post-meta-item-icon">
                  <i class="fa fa-file-word-o"></i>
                </span>
                
                  <span class="post-meta-item-text">字数统计&#58;</span>
                
                <span title="字数统计">
                  2.8k words
                </span>
              

              
                <span class="post-meta-divider">|</span>
              

              
                <span class="post-meta-item-icon">
                  <i class="fa fa-clock-o"></i>
                </span>
                
                  <span class="post-meta-item-text">阅读时长 &asymp;</span>
                
                <span title="阅读时长">
                  9 min
                </span>
              
            </div>
          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <h2 id="搭架自己的微前端"><a href="#搭架自己的微前端" class="headerlink" title="搭架自己的微前端"></a>搭架自己的微前端</h2><h3 id="软件设计原则"><a href="#软件设计原则" class="headerlink" title="软件设计原则"></a>软件设计原则</h3><h4 id="solid"><a href="#solid" class="headerlink" title="solid"></a>solid</h4><h4 id="额外设计原则"><a href="#额外设计原则" class="headerlink" title="额外设计原则"></a>额外设计原则</h4><ul>
<li><p>组合/聚合复用原则</p>
<ul>
<li>组合优于继承</li>
<li>23种设计模式</li>
</ul>
</li>
<li><p>无环依赖</p>
<ul>
<li>A-&gt;B-&gt;C-&gt;A 中介者模式</li>
</ul>
</li>
<li><p>共同封装</p>
<ul>
<li>将变化隔离 开放-封闭的拓展</li>
</ul>
</li>
<li><p>共同重用</p>
</li>
<li><p>好莱坞原则</p>
<ul>
<li>控制反转（依赖注入）npm包的思想</li>
</ul>
</li>
<li><p>dry原则（不重复）</p>
<ul>
<li>利用以上的设计原则去做封装</li>
</ul>
</li>
<li><p>保持简单和傻瓜</p>
<ul>
<li>保持页面简洁 功能实用 操作方便</li>
</ul>
</li>
<li><p>高内聚低耦合</p>
</li>
<li><p>关注点分离</p>
</li>
<li><p>你不需要它</p>
<ul>
<li>架构前期不要过度设计</li>
<li>要足够简单，不失拓展性</li>
</ul>
<h3 id="软件设计分层"><a href="#软件设计分层" class="headerlink" title="软件设计分层"></a>软件设计分层</h3><h4 id="系统级架构"><a href="#系统级架构" class="headerlink" title="系统级架构"></a>系统级架构</h4></li>
<li><p><strong>定义</strong> 应用在这个系统中，如何与后端通信，如何和第三方集成</p>
</li>
<li><p><strong>处理</strong></p>
<ul>
<li>设计前端<ul>
<li>首要：了解前端系统与其他系统之间的关系</li>
<li>包括业务关系和协作机制</li>
</ul>
</li>
<li>设计后端<ul>
<li>规定与后端数据通信的机制</li>
<li>包括api设计，token设计，数据传递设计cookie</li>
</ul>
</li>
<li>分离设计<ul>
<li>如何实施技术决策，用户鉴权，APi接口管理，Api文档管理，Mock使用，BFF，是否需要服务端渲染</li>
</ul>
</li>
</ul>
</li>
<li><p><strong>微前端</strong></p>
<ul>
<li><p><strong>定义</strong></p>
<ul>
<li>在一个系统内微前端是应用间的架构方案</li>
<li>在多个应用间，微前端是一种系统间等架构方案，</li>
<li>微前端是将多个前端应用以某种形式结合在一起进行应用。</li>
</ul>
</li>
<li><p><strong>解决</strong></p>
<ul>
<li>单体应用在长时间跨度，变成了一个巨石应用后难以维护。</li>
</ul>
</li>
<li><p><strong>实现形式</strong></p>
<ul>
<li>单实例，</li>
<li>多实例 同一时刻可以展示多个子应用<ul>
<li>通常使用web components方案做子应用封装，子应用更像业务组件而不是应用</li>
</ul>
</li>
</ul>
<h4 id="应用级架构"><a href="#应用级架构" class="headerlink" title="应用级架构"></a>应用级架构</h4></li>
</ul>
</li>
<li><p><strong>定义</strong> 系统级架构的细化</p>
<ul>
<li>单个应用与其他外部应用的关系，微服务架构下多个应用的协作，数据交换</li>
</ul>
</li>
<li><p><strong>应用</strong></p>
<ul>
<li>脚手架 应用生成</li>
<li>模式库 ui库 函数库</li>
<li>设计系统 内部实现的功能，与外部的信息交互<h4 id="模块级架构"><a href="#模块级架构" class="headerlink" title="模块级架构"></a>模块级架构</h4></li>
</ul>
</li>
<li><p><strong>定义</strong> 业务编码前的设计 类似迭代</p>
<h4 id="代码级架构"><a href="#代码级架构" class="headerlink" title="代码级架构"></a>代码级架构</h4></li>
<li><p><strong>定义</strong> 规范与原则</p>
<ul>
<li>开发流程</li>
<li>代码质量与改善</li>
<li>规范而非默契<h4 id="注意"><a href="#注意" class="headerlink" title="注意"></a>注意</h4></li>
<li>开发中，要注意可维护性</li>
<li>简单的代码可维护性高，越抽象越难维护。</li>
</ul>
</li>
</ul>
<h3 id="如何保证架构的质量-稳定性和健壮性"><a href="#如何保证架构的质量-稳定性和健壮性" class="headerlink" title="如何保证架构的质量-稳定性和健壮性"></a>如何保证架构的质量-稳定性和健壮性</h3><ul>
<li><p>系统的稳定性</p>
<ul>
<li>系统处于平衡状态，收到外部影响后，能够回到原来的平衡状态</li>
<li>意义<ul>
<li>架构设计的基石</li>
<li>可以更好实现自我修复</li>
</ul>
</li>
</ul>
</li>
<li><p>系统的健壮性</p>
<ul>
<li>计算机软件在输入错误，磁盘故障，网络过载或攻击下，不崩溃</li>
<li>容错能力强，运行不易被干扰，安全性好</li>
<li>衡量<ul>
<li>从错误的输入推断出正确合理的输入</li>
<li>可以正确运行在不同环境下</li>
<li>检测内部的设计或者编码错误，并且得到正确结果</li>
</ul>
</li>
</ul>
</li>
<li><p>含义</p>
<ul>
<li>对特定软件的自身要求</li>
<li>稳定性和健壮性是软件处理的一部分内容</li>
<li>稳定性和健壮性是软件规划时确定的目标</li>
</ul>
<h4 id="衡量架构质量"><a href="#衡量架构质量" class="headerlink" title="衡量架构质量"></a>衡量架构质量</h4></li>
<li><p>拓展性</p>
</li>
<li><p>维护性</p>
</li>
<li><p>可管理</p>
</li>
<li><p>高可用（故障修复，容灾，降级，熔断）</p>
</li>
</ul>
<h4 id="如何保证架构质量"><a href="#如何保证架构质量" class="headerlink" title="如何保证架构质量"></a>如何保证架构质量</h4><ul>
<li>理解难度，更好理解</li>
<li>接入依赖的成本，易于接入</li>
<li>奔溃率和错误率的指标 低</li>
<li>开发效率 提高开发效率</li>
<li>错误上报和信息收集功能 </li>
</ul>
<h4 id="架构前期准备（正确的选择是良好的开端）"><a href="#架构前期准备（正确的选择是良好的开端）" class="headerlink" title="架构前期准备（正确的选择是良好的开端）"></a>架构前期准备（正确的选择是良好的开端）</h4><ul>
<li>架构师分类<ul>
<li>系统架构师 偏向技术<ul>
<li>从系统维度，负责整理系统的架构设计</li>
<li>主要是基础服务和各系统间协调，着眼全局</li>
<li>比如关注负载，可靠性，伸缩，扩展，整体项目切分，缓存应用方面的基础架构设计</li>
</ul>
</li>
<li>应用架构师 业务技术并存<ul>
<li>从应用程序的维度，负责某个应用的技术架构，偏向业务</li>
<li>关注理解业务，梳理模型，设计模式，接口，数据交互等</li>
<li>了解业务系统的流转</li>
</ul>
</li>
<li>业务架构师 脱离开发<ul>
<li>从业务流程维度，关注某行业，业务的领域，获得领域模型，最终获得系统模型</li>
<li>页脚业务领域专家，行业专家，产品咨询师，资深顾问</li>
</ul>
</li>
</ul>
</li>
<li>技术前期准备<ul>
<li>技术选型：社区氛围，发展规模，未来发展趋势，与团队契合度，执行成本，维护和迁移成本，执行效率调研和报告</li>
<li>充分调研每一项技术可能的利和弊</li>
<li>最大程度预测架构设计中的缺陷，防止问题发生</li>
</ul>
</li>
<li>技术优化<ul>
<li>发展过程中，可能存在有些有悖于当前架构的设计，早餐架构发展阻塞，所以需要进行架构优化，提高适应性</li>
</ul>
</li>
<li>架构优化<ul>
<li>不是一蹴而就的，在业务发函中，架构也在不断演进</li>
<li>对架构设计进行实时调休，使架构优化成为常态</li>
<li>通过不断调整，改进不足，补足短板<h4 id="技术债务填补和崩溃预防"><a href="#技术债务填补和崩溃预防" class="headerlink" title="技术债务填补和崩溃预防"></a>技术债务填补和崩溃预防</h4></li>
<li>原因<ul>
<li>开发过程中，因为时间紧迫导致实现不合理<ul>
<li>算法优化，</li>
</ul>
</li>
<li>暂时没有想好更好的实现方式而妥协<ul>
<li>if else</li>
</ul>
</li>
<li>架构设计前期没有考虑到的细节<ul>
<li>参数传递 -&gt; 状态管理</li>
</ul>
</li>
<li>不合理的交互设计，导致技术实现复杂</li>
<li>旧功能文档缺失，无正确拓展，修改和兼容旧功能，导致上线后问题剧增。 </li>
</ul>
</li>
<li>后果<ul>
<li>修复变重构 小的技术债务不偿还，最后会演变为大规模的重构</li>
<li>影响开发速度 极大影响上线速度，迭代慢，失去核心竞争力</li>
<li>陷入维护开发的恶性循环</li>
</ul>
</li>
<li>技术填补<ul>
<li>思想<ul>
<li>优秀的架构设计</li>
<li>有效处理当前需求可预计的情况，</li>
</ul>
</li>
<li>解决方案<ul>
<li>根据当前业务，进行项目拆分，尽可能代码解耦合<ul>
<li>必须要日志系统，操作日志，错误日志，业务日志</li>
</ul>
</li>
<li>良好的技术培训，和传帮带能力<ul>
<li>让每一个开发者可以从更深一层次理解自己实现的功能</li>
<li>从最开始的代码规范，到熟悉业务，最后在编写文档 </li>
</ul>
</li>
<li>充分的技术方案<ul>
<li>充分理解需求，产生的最理想的实现方案</li>
</ul>
</li>
<li>不同工程师之间相互review代码<ul>
<li>codeReview也是对自身的一个提高</li>
</ul>
</li>
<li>提升对修复技术债务重要性的认知<ul>
<li>遇见一个债务导致的问题，才能花时间去解决</li>
</ul>
</li>
<li>善于发现和定期处理一些技术债务<ul>
<li>用于发现系统中的技术债务，让自己对系统负责</li>
</ul>
</li>
</ul>
</li>
<li>总结<ul>
<li>上线后，时间不紧，找时间处理技术债务，一遍建立感情和品位原来的代码</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>
<h4 id="预防架构崩溃"><a href="#预防架构崩溃" class="headerlink" title="预防架构崩溃"></a>预防架构崩溃</h4><ul>
<li>系统崩溃的产生<ul>
<li>日志记录。操作日志，错误日志，业务日志</li>
</ul>
</li>
<li>预防崩溃<ul>
<li>用户行为抓取–争取最新时间获取用户操作链条</li>
<li>解决存量问题-技术债务</li>
<li>遏制新增，减少新增问题的概率</li>
<li>对脏数据进行兜底和检验</li>
<li>单元测试</li>
<li>崩溃报警</li>
<li>自动化测试，整体流程自动化测试</li>
<li>更广的灰度触达</li>
<li>性能优化体系</li>
</ul>
</li>
</ul>
<h4 id="系统重构"><a href="#系统重构" class="headerlink" title="系统重构"></a>系统重构</h4><ul>
<li>起因<ul>
<li>架构不是永恒不变的，也是有生命周期的。</li>
</ul>
</li>
<li>重构概念<ul>
<li>对软件内部结构的一种调整</li>
<li>在不改变当前软件的可观察行为的前提下，提高其可理解性，降低修改成本。</li>
</ul>
</li>
<li>实现方式<ul>
<li>使用一系列重构手法，调整其结构</li>
<li>理念<ul>
<li>运用大量微小的保持软件行为的步骤，一步步达成大规模的修改。</li>
</ul>
</li>
</ul>
</li>
<li>早期系统优势<ul>
<li>开发速度快</li>
<li>代码复杂度低</li>
<li>代码规范保持完好</li>
<li>严格注重开发规范，不会允许危及架构设计的代码产生</li>
<li>早期功能简单，拓展起来难度低，成本低</li>
</ul>
</li>
<li>后期系统<ul>
<li>具备所有早期其他的劣势</li>
<li>代码复杂度高</li>
<li>代码规范不完善</li>
<li>很多需求和功能出现逾越架构设计的情况</li>
<li>添加新功能兼顾较多，涉及较多模块</li>
</ul>
</li>
<li>流程<ul>
<li>确定问题点，确定重构功能和范围</li>
<li>旧架构设计和逻辑梳理</li>
</ul>
</li>
</ul>
<h3 id="实现方式对比"><a href="#实现方式对比" class="headerlink" title="实现方式对比"></a>实现方式对比</h3><ol>
<li>iframe</li>
</ol>
<ul>
<li>优势<ul>
<li>技术成熟</li>
<li>支持页面嵌入</li>
<li>天然支持运行沙箱隔离，独立运行</li>
</ul>
</li>
<li>劣势<ul>
<li>页面之间可以是不同域名 鉴权</li>
<li>设计一套通讯机制，如何监听，传参</li>
<li>应用加载，渲染，缓存的实现，都在浏览器中难以控制</li>
</ul>
</li>
</ul>
<ol start="2">
<li>web component</li>
</ol>
<ul>
<li>优势<ul>
<li>支持自定义元素，</li>
<li>支持shadow dom，可通过关联进行控制</li>
<li>支持模板template和插槽slot，引入自定义组件</li>
</ul>
</li>
<li>劣势<ul>
<li>接入微前端需要重写当前项目</li>
<li>生态系统不完善，技术过新容易出现兼容性</li>
<li>架构设计复杂，组件间拆分过细，通讯控制繁琐</li>
</ul>
</li>
</ul>
<ol start="3">
<li>自研框架</li>
</ol>
<ul>
<li>优势<ul>
<li>高度定制化，满足兼容的一切场景</li>
<li>独立通信机制和沙箱运行环境，可解决应用之间相互影响的问题</li>
<li>支持不同技术栈的子应用，可无缝实现页面无刷新渲染。</li>
</ul>
</li>
<li>劣势<ul>
<li>技术实现难度较高</li>
<li>需要设计一套定制的通信机制</li>
<li>首次加载会出现资源过大的情况</li>
</ul>
</li>
<li>实现<ul>
<li>路由分发式</li>
<li>主应用控制路由匹配和子应用加载，共享依赖加载</li>
<li>子应用做功能，接入主应用实现主子应用控制和联动</li>
</ul>
</li>
</ul>
<h3 id="技术选型"><a href="#技术选型" class="headerlink" title="技术选型"></a>技术选型</h3><ul>
<li>主应用  vue3</li>
<li>子应用<ul>
<li>vue2</li>
<li>vue3</li>
<li>react16</li>
<li>solid.js</li>
</ul>
</li>
<li>后端服务和发布应用<ul>
<li>koa实现</li>
</ul>
</li>
<li>发布应用<ul>
<li>express</li>
</ul>
</li>
<li>微前端core js node</li>
</ul>
<h3 id="绘制项目架构图"><a href="#绘制项目架构图" class="headerlink" title="绘制项目架构图"></a>绘制项目架构图</h3><ul>
<li><p>分析需求</p>
<ul>
<li>主子应用功能</li>
</ul>
</li>
<li><p>主应用</p>
<ul>
<li>注册子应用</li>
<li>加载、渲染子应用</li>
<li>路由匹配（activeWhen， rules - 由框架判断）</li>
<li>获取数据（公共依赖，通过数据做鉴权处理）</li>
<li>通信（父子通信，子父通信）</li>
</ul>
</li>
<li><p>子应用</p>
<ul>
<li>渲染</li>
<li>监听通信（主应用传递的数据，做更新）</li>
</ul>
</li>
<li><p>微前端框架</p>
<ul>
<li>子应用的注册</li>
<li>有开始内容（应用加载完成）</li>
<li>路由更新判断</li>
<li>匹配对应的子应用</li>
<li>加载子应用内容</li>
<li>完成所欲依赖项的执行</li>
<li>将子应用渲染在固定容器内</li>
<li>公共事件的管理</li>
<li>异常的捕获和报错</li>
<li>全局状态管理</li>
<li>沙箱隔离</li>
<li>通信机制</li>
</ul>
</li>
<li><p>服务端功能</p>
<ul>
<li>提供数据服务</li>
</ul>
</li>
<li><p>发布平台</p>
<ul>
<li>主子应用的打包和发布</li>
</ul>
</li>
</ul>
<h4 id="微前端架构图"><a href="#微前端架构图" class="headerlink" title="微前端架构图"></a>微前端架构图</h4><p><img src="/images/%E5%B7%A5%E7%A8%8B%E5%8C%96/%E5%BE%AE%E5%89%8D%E7%AB%AF%E6%9E%B6%E6%9E%84%E5%9B%BE.png"></p>

      
    </div>
    
    
    

    

    

    
      <div>
        <ul class="post-copyright">
  <li class="post-copyright-author">
    <strong>本文作者：</strong>
    刘圣伟
  </li>
  <li class="post-copyright-link">
    <strong>本文链接：</strong>
    <a href="https://swliu920322.github.io/2021/05/08/%E5%B7%A5%E7%A8%8B%E5%8C%96/%E5%BE%AE%E5%89%8D%E7%AB%AF/%E5%BE%AE%E5%89%8D%E7%AB%AF-%E6%9E%B6%E6%9E%84%E5%9F%BA%E7%A1%80/" title="搭架自己的微前端">https://swliu920322.github.io/2021/05/08/%E5%B7%A5%E7%A8%8B%E5%8C%96/%E5%BE%AE%E5%89%8D%E7%AB%AF/%E5%BE%AE%E5%89%8D%E7%AB%AF-%E6%9E%B6%E6%9E%84%E5%9F%BA%E7%A1%80/</a>
  </li>
  <li class="post-copyright-license">
    <strong>版权声明： </strong>
    本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/3.0/" rel="external nofollow" target="_blank">CC BY-NC-SA 3.0</a> 许可协议。转载请注明出处！
  </li>
</ul>

      </div>
    

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/tags/%E6%8A%80%E6%9C%AF/" rel="tag"># 技术</a>
          
            <a href="/tags/%E5%BE%AE%E5%89%8D%E7%AB%AF/" rel="tag"># 微前端</a>
          
            <a href="/tags/%E6%9E%B6%E6%9E%84/" rel="tag"># 架构</a>
          
        </div>
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2021/03/20/%E6%8A%80%E6%9C%AF/%E9%97%AD%E5%8C%85%E5%9C%A8vue3%E7%9A%84%E4%BD%BF%E7%94%A8/" rel="next" title="闭包在vue3的使用">
                <i class="fa fa-chevron-left"></i> 闭包在vue3的使用
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2021/05/08/%E5%B7%A5%E7%A8%8B%E5%8C%96/%E5%BE%AE%E5%89%8D%E7%AB%AF/%E5%BE%AE%E5%89%8D%E7%AB%AF-%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F/" rel="prev" title="搭架自己的微前端-实现方式">
                搭架自己的微前端-实现方式 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          

  
    <div class="comments" id="comments">
    </div>
  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <img class="site-author-image" itemprop="image"
                src="/%5Bobject%20Object%5D"
                alt="" />
            
              <p class="site-author-name" itemprop="name"></p>
              <p class="site-description motion-element" itemprop="description"></p>
          </div>

          <nav class="site-state motion-element">

            
              <div class="site-state-item site-state-posts">
              
                <a href="/archives/%7C%7C%20archive">
              
                  <span class="site-state-item-count">35</span>
                  <span class="site-state-item-name">日志</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-categories">
                <a href="/categories/index.html">
                  <span class="site-state-item-count">12</span>
                  <span class="site-state-item-name">分类</span>
                </a>
              </div>
            

            
              
              
              <div class="site-state-item site-state-tags">
                <a href="/tags/index.html">
                  <span class="site-state-item-count">44</span>
                  <span class="site-state-item-name">标签</span>
                </a>
              </div>
            

          </nav>

          

          
            <div class="links-of-author motion-element">
                
                  <span class="links-of-author-item">
                    <a href="https://github.com/swliu920322" target="_blank" title="GitHub">
                      
                        <i class="fa fa-fw fa-github"></i>GitHub</a>
                  </span>
                
                  <span class="links-of-author-item">
                    <a href="mailto:1064084779@@qq.com" target="_blank" title="E-Mail">
                      
                        <i class="fa fa-fw fa-envelope"></i>E-Mail</a>
                  </span>
                
            </div>
          

          
          

          
          

          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E6%90%AD%E6%9E%B6%E8%87%AA%E5%B7%B1%E7%9A%84%E5%BE%AE%E5%89%8D%E7%AB%AF"><span class="nav-number">1.</span> <span class="nav-text">搭架自己的微前端</span></a><ol class="nav-child"><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BD%AF%E4%BB%B6%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99"><span class="nav-number">1.1.</span> <span class="nav-text">软件设计原则</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#solid"><span class="nav-number">1.1.1.</span> <span class="nav-text">solid</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%9D%E5%A4%96%E8%AE%BE%E8%AE%A1%E5%8E%9F%E5%88%99"><span class="nav-number">1.1.2.</span> <span class="nav-text">额外设计原则</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E8%BD%AF%E4%BB%B6%E8%AE%BE%E8%AE%A1%E5%88%86%E5%B1%82"><span class="nav-number">1.2.</span> <span class="nav-text">软件设计分层</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E7%BA%A7%E6%9E%B6%E6%9E%84"><span class="nav-number">1.2.1.</span> <span class="nav-text">系统级架构</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%BA%94%E7%94%A8%E7%BA%A7%E6%9E%B6%E6%9E%84"><span class="nav-number">1.2.2.</span> <span class="nav-text">应用级架构</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%A8%A1%E5%9D%97%E7%BA%A7%E6%9E%B6%E6%9E%84"><span class="nav-number">1.2.3.</span> <span class="nav-text">模块级架构</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E4%BB%A3%E7%A0%81%E7%BA%A7%E6%9E%B6%E6%9E%84"><span class="nav-number">1.2.4.</span> <span class="nav-text">代码级架构</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%B3%A8%E6%84%8F"><span class="nav-number">1.2.5.</span> <span class="nav-text">注意</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E4%BF%9D%E8%AF%81%E6%9E%B6%E6%9E%84%E7%9A%84%E8%B4%A8%E9%87%8F-%E7%A8%B3%E5%AE%9A%E6%80%A7%E5%92%8C%E5%81%A5%E5%A3%AE%E6%80%A7"><span class="nav-number">1.3.</span> <span class="nav-text">如何保证架构的质量-稳定性和健壮性</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E8%A1%A1%E9%87%8F%E6%9E%B6%E6%9E%84%E8%B4%A8%E9%87%8F"><span class="nav-number">1.3.1.</span> <span class="nav-text">衡量架构质量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%A6%82%E4%BD%95%E4%BF%9D%E8%AF%81%E6%9E%B6%E6%9E%84%E8%B4%A8%E9%87%8F"><span class="nav-number">1.3.2.</span> <span class="nav-text">如何保证架构质量</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%9E%B6%E6%9E%84%E5%89%8D%E6%9C%9F%E5%87%86%E5%A4%87%EF%BC%88%E6%AD%A3%E7%A1%AE%E7%9A%84%E9%80%89%E6%8B%A9%E6%98%AF%E8%89%AF%E5%A5%BD%E7%9A%84%E5%BC%80%E7%AB%AF%EF%BC%89"><span class="nav-number">1.3.3.</span> <span class="nav-text">架构前期准备（正确的选择是良好的开端）</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E6%8A%80%E6%9C%AF%E5%80%BA%E5%8A%A1%E5%A1%AB%E8%A1%A5%E5%92%8C%E5%B4%A9%E6%BA%83%E9%A2%84%E9%98%B2"><span class="nav-number">1.3.4.</span> <span class="nav-text">技术债务填补和崩溃预防</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E9%A2%84%E9%98%B2%E6%9E%B6%E6%9E%84%E5%B4%A9%E6%BA%83"><span class="nav-number">1.3.5.</span> <span class="nav-text">预防架构崩溃</span></a></li><li class="nav-item nav-level-4"><a class="nav-link" href="#%E7%B3%BB%E7%BB%9F%E9%87%8D%E6%9E%84"><span class="nav-number">1.3.6.</span> <span class="nav-text">系统重构</span></a></li></ol></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E5%AE%9E%E7%8E%B0%E6%96%B9%E5%BC%8F%E5%AF%B9%E6%AF%94"><span class="nav-number">1.4.</span> <span class="nav-text">实现方式对比</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E6%8A%80%E6%9C%AF%E9%80%89%E5%9E%8B"><span class="nav-number">1.5.</span> <span class="nav-text">技术选型</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#%E7%BB%98%E5%88%B6%E9%A1%B9%E7%9B%AE%E6%9E%B6%E6%9E%84%E5%9B%BE"><span class="nav-number">1.6.</span> <span class="nav-text">绘制项目架构图</span></a><ol class="nav-child"><li class="nav-item nav-level-4"><a class="nav-link" href="#%E5%BE%AE%E5%89%8D%E7%AB%AF%E6%9E%B6%E6%9E%84%E5%9B%BE"><span class="nav-number">1.6.1.</span> <span class="nav-text">微前端架构图</span></a></li></ol></li></ol></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      
        <div class="back-to-top">
          <i class="fa fa-arrow-up"></i>
          
            <span id="scrollpercent"><span>0</span>%</span>
          
        </div>
      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; 2021 &mdash; <span itemprop="copyrightYear">2022</span>
  <span class="with-love">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">刘圣伟</span>

  
</div>









        
<div class="busuanzi-count">
  <script async src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

  
    <span class="site-uv">
      <i class="fa fa-user"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_uv"></span>
      
    </span>
  

  
    <span class="site-pv">
      <i class="fa fa-eye"></i>
      <span class="busuanzi-value" id="busuanzi_value_site_pv"></span>
      
    </span>
  
</div>








        
      </div>
    </footer>

    

    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  




  
  









  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/fastclick/lib/fastclick.min.js?v=1.0.6"></script>
  

  
  
    <script type="text/javascript" src="/lib/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/fancybox/source/jquery.fancybox.pack.js?v=2.1.5"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three.min.js"></script>
  

  
  
    <script type="text/javascript" src="/lib/three/three-waves.min.js"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=5.1.4"></script>



  
  


  <script type="text/javascript" src="/js/src/affix.js?v=5.1.4"></script>

  <script type="text/javascript" src="/js/src/schemes/pisces.js?v=5.1.4"></script>



  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=5.1.4"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=5.1.4"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=5.1.4"></script>



  


  




	





  





  










  <script src="//cdn1.lncld.net/static/js/3.0.4/av-min.js"></script>
  <script src="//unpkg.com/valine/dist/Valine.min.js"></script>
  
  <script type="text/javascript">
    var GUEST = ['nick','mail','link'];
    var guest = 'nick,mail,link';
    guest = guest.split(',').filter(item=>{
      return GUEST.indexOf(item)>-1;
    });
    new Valine({
        el: '#comments' ,
        verify: false,
        notify: false,
        appId: 'EBpGzq6eYoqhgaJQGIykcFED-gzGzoHsz',
        appKey: 'drIOSv99SkzYrgQuRx3CCyxH',
        placeholder: '客官，说点什么吧',
        avatar:'mm',
        guest_info:guest,
        pageSize:'10' || 10,
    });
  </script>



  

  <script type="text/javascript">
    // Popup Window;
    var isfetched = false;
    var isXml = true;
    // Search DB path;
    var search_path = "search.xml";
    if (search_path.length === 0) {
      search_path = "search.xml";
    } else if (/json$/i.test(search_path)) {
      isXml = false;
    }
    var path = "/" + search_path;
    // monitor main search box;

    var onPopupClose = function (e) {
      $('.popup').hide();
      $('#local-search-input').val('');
      $('.search-result-list').remove();
      $('#no-result').remove();
      $(".local-search-pop-overlay").remove();
      $('body').css('overflow', '');
    }

    function proceedsearch() {
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay"></div>')
        .css('overflow', 'hidden');
      $('.search-popup-overlay').click(onPopupClose);
      $('.popup').toggle();
      var $localSearchInput = $('#local-search-input');
      $localSearchInput.attr("autocapitalize", "none");
      $localSearchInput.attr("autocorrect", "off");
      $localSearchInput.focus();
    }

    // search function;
    var searchFunc = function(path, search_id, content_id) {
      'use strict';

      // start loading animation
      $("body")
        .append('<div class="search-popup-overlay local-search-pop-overlay">' +
          '<div id="search-loading-icon">' +
          '<i class="fa fa-spinner fa-pulse fa-5x fa-fw"></i>' +
          '</div>' +
          '</div>')
        .css('overflow', 'hidden');
      $("#search-loading-icon").css('margin', '20% auto 0 auto').css('text-align', 'center');

      $.ajax({
        url: path,
        dataType: isXml ? "xml" : "json",
        async: true,
        success: function(res) {
          // get the contents from search data
          isfetched = true;
          $('.popup').detach().appendTo('.header-inner');
          var datas = isXml ? $("entry", res).map(function() {
            return {
              title: $("title", this).text(),
              content: $("content",this).text(),
              url: $("url" , this).text()
            };
          }).get() : res;
          var input = document.getElementById(search_id);
          var resultContent = document.getElementById(content_id);
          var inputEventFunction = function() {
            var searchText = input.value.trim().toLowerCase();
            var keywords = searchText.split(/[\s\-]+/);
            if (keywords.length > 1) {
              keywords.push(searchText);
            }
            var resultItems = [];
            if (searchText.length > 0) {
              // perform local searching
              datas.forEach(function(data) {
                var isMatch = false;
                var hitCount = 0;
                var searchTextCount = 0;
                var title = data.title.trim();
                var titleInLowerCase = title.toLowerCase();
                var content = data.content.trim().replace(/<[^>]+>/g,"");
                var contentInLowerCase = content.toLowerCase();
                var articleUrl = decodeURIComponent(data.url);
                var indexOfTitle = [];
                var indexOfContent = [];
                // only match articles with not empty titles
                if(title != '') {
                  keywords.forEach(function(keyword) {
                    function getIndexByWord(word, text, caseSensitive) {
                      var wordLen = word.length;
                      if (wordLen === 0) {
                        return [];
                      }
                      var startPosition = 0, position = [], index = [];
                      if (!caseSensitive) {
                        text = text.toLowerCase();
                        word = word.toLowerCase();
                      }
                      while ((position = text.indexOf(word, startPosition)) > -1) {
                        index.push({position: position, word: word});
                        startPosition = position + wordLen;
                      }
                      return index;
                    }

                    indexOfTitle = indexOfTitle.concat(getIndexByWord(keyword, titleInLowerCase, false));
                    indexOfContent = indexOfContent.concat(getIndexByWord(keyword, contentInLowerCase, false));
                  });
                  if (indexOfTitle.length > 0 || indexOfContent.length > 0) {
                    isMatch = true;
                    hitCount = indexOfTitle.length + indexOfContent.length;
                  }
                }

                // show search results

                if (isMatch) {
                  // sort index by position of keyword

                  [indexOfTitle, indexOfContent].forEach(function (index) {
                    index.sort(function (itemLeft, itemRight) {
                      if (itemRight.position !== itemLeft.position) {
                        return itemRight.position - itemLeft.position;
                      } else {
                        return itemLeft.word.length - itemRight.word.length;
                      }
                    });
                  });

                  // merge hits into slices

                  function mergeIntoSlice(text, start, end, index) {
                    var item = index[index.length - 1];
                    var position = item.position;
                    var word = item.word;
                    var hits = [];
                    var searchTextCountInSlice = 0;
                    while (position + word.length <= end && index.length != 0) {
                      if (word === searchText) {
                        searchTextCountInSlice++;
                      }
                      hits.push({position: position, length: word.length});
                      var wordEnd = position + word.length;

                      // move to next position of hit

                      index.pop();
                      while (index.length != 0) {
                        item = index[index.length - 1];
                        position = item.position;
                        word = item.word;
                        if (wordEnd > position) {
                          index.pop();
                        } else {
                          break;
                        }
                      }
                    }
                    searchTextCount += searchTextCountInSlice;
                    return {
                      hits: hits,
                      start: start,
                      end: end,
                      searchTextCount: searchTextCountInSlice
                    };
                  }

                  var slicesOfTitle = [];
                  if (indexOfTitle.length != 0) {
                    slicesOfTitle.push(mergeIntoSlice(title, 0, title.length, indexOfTitle));
                  }

                  var slicesOfContent = [];
                  while (indexOfContent.length != 0) {
                    var item = indexOfContent[indexOfContent.length - 1];
                    var position = item.position;
                    var word = item.word;
                    // cut out 100 characters
                    var start = position - 20;
                    var end = position + 80;
                    if(start < 0){
                      start = 0;
                    }
                    if (end < position + word.length) {
                      end = position + word.length;
                    }
                    if(end > content.length){
                      end = content.length;
                    }
                    slicesOfContent.push(mergeIntoSlice(content, start, end, indexOfContent));
                  }

                  // sort slices in content by search text's count and hits' count

                  slicesOfContent.sort(function (sliceLeft, sliceRight) {
                    if (sliceLeft.searchTextCount !== sliceRight.searchTextCount) {
                      return sliceRight.searchTextCount - sliceLeft.searchTextCount;
                    } else if (sliceLeft.hits.length !== sliceRight.hits.length) {
                      return sliceRight.hits.length - sliceLeft.hits.length;
                    } else {
                      return sliceLeft.start - sliceRight.start;
                    }
                  });

                  // select top N slices in content

                  var upperBound = parseInt('1');
                  if (upperBound >= 0) {
                    slicesOfContent = slicesOfContent.slice(0, upperBound);
                  }

                  // highlight title and content

                  function highlightKeyword(text, slice) {
                    var result = '';
                    var prevEnd = slice.start;
                    slice.hits.forEach(function (hit) {
                      result += text.substring(prevEnd, hit.position);
                      var end = hit.position + hit.length;
                      result += '<b class="search-keyword">' + text.substring(hit.position, end) + '</b>';
                      prevEnd = end;
                    });
                    result += text.substring(prevEnd, slice.end);
                    return result;
                  }

                  var resultItem = '';

                  if (slicesOfTitle.length != 0) {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + highlightKeyword(title, slicesOfTitle[0]) + "</a>";
                  } else {
                    resultItem += "<li><a href='" + articleUrl + "' class='search-result-title'>" + title + "</a>";
                  }

                  slicesOfContent.forEach(function (slice) {
                    resultItem += "<a href='" + articleUrl + "'>" +
                      "<p class=\"search-result\">" + highlightKeyword(content, slice) +
                      "...</p>" + "</a>";
                  });

                  resultItem += "</li>";
                  resultItems.push({
                    item: resultItem,
                    searchTextCount: searchTextCount,
                    hitCount: hitCount,
                    id: resultItems.length
                  });
                }
              })
            };
            if (keywords.length === 1 && keywords[0] === "") {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-search fa-5x" /></div>'
            } else if (resultItems.length === 0) {
              resultContent.innerHTML = '<div id="no-result"><i class="fa fa-frown-o fa-5x" /></div>'
            } else {
              resultItems.sort(function (resultLeft, resultRight) {
                if (resultLeft.searchTextCount !== resultRight.searchTextCount) {
                  return resultRight.searchTextCount - resultLeft.searchTextCount;
                } else if (resultLeft.hitCount !== resultRight.hitCount) {
                  return resultRight.hitCount - resultLeft.hitCount;
                } else {
                  return resultRight.id - resultLeft.id;
                }
              });
              var searchResultList = '<ul class=\"search-result-list\">';
              resultItems.forEach(function (result) {
                searchResultList += result.item;
              })
              searchResultList += "</ul>";
              resultContent.innerHTML = searchResultList;
            }
          }

          if ('auto' === 'auto') {
            input.addEventListener('input', inputEventFunction);
          } else {
            $('.search-icon').click(inputEventFunction);
            input.addEventListener('keypress', function (event) {
              if (event.keyCode === 13) {
                inputEventFunction();
              }
            });
          }

          // remove loading animation
          $(".local-search-pop-overlay").remove();
          $('body').css('overflow', '');

          proceedsearch();
        }
      });
    }

    // handle and trigger popup window;
    $('.popup-trigger').click(function(e) {
      e.stopPropagation();
      if (isfetched === false) {
        searchFunc(path, 'local-search-input', 'local-search-result');
      } else {
        proceedsearch();
      };
    });

    $('.popup-btn-close').click(onPopupClose);
    $('.popup').click(function(e){
      e.stopPropagation();
    });
    $(document).on('keyup', function (event) {
      var shouldDismissSearchPopup = event.which === 27 &&
        $('.search-popup').is(':visible');
      if (shouldDismissSearchPopup) {
        onPopupClose();
      }
    });
  </script>





  

  

  

  
  

  

  
  <script type="text/javascript" src="/js/src/js.cookie.js?v=5.1.4"></script>
  <script type="text/javascript" src="/js/src/scroll-cookie.js?v=5.1.4"></script>


  

</body>
</html>
